class ManKey{
    static ModManKeyEvent=
        {
            EVT_KEY_DOWN: "EVT_KEY_DOWN",
            EVT_KEY_UP:"EVT_KEY_UP",
            EVT_CLICK: "EVT_CLICK",
            EVT_DBL_CLICK: "EVT_DBL_CLICK",
            EVT_PRESSING: "EVT_PRESSING",
            EVT_LONG_CLICK:"EVT_LONG_CLICK",
        }
    static ModManKeyState=
        {
            STT_IDLE:0,
            STT_WAITING_CLICK_UP:1,
            STT_WAITING_DBL_CLICK_DOWN :2,
            STT_WAITING_DBL_CLICK_UP:3,
            STT_LONG_PRESSING:10
        }

    m_state;
    m_downMs;
    m_pressingCount;
    constructor(id,postEvent) {
        this.keyId=id;
        this.keyVal = false;
        this.m_downMs=0;
        this.m_pressingCount=0;
        this.postEvent=postEvent;
    }
    Button_Down()
    {
        this.keyVal = true;
    }

    Button_Up()
    {
        this.keyVal = false;
    }

    OnUiTick(ms){
        let  keyPressState = this.keyVal;
        switch ( this.m_state)
        {
            case  ManKey.ModManKeyState.STT_IDLE: // 缺省处于空闲状态
            default:
                if (keyPressState == true)
                {  // 一切从按下开始
                    this.m_downMs = ms;
                    this.postEvent(this.keyId, ManKey.ModManKeyEvent.EVT_KEY_DOWN);
                    this.m_state = ManKey.ModManKeyState.STT_WAITING_CLICK_UP;
                }
                break;
            case  ManKey.ModManKeyState.STT_WAITING_CLICK_UP:
                if (ms - this.m_downMs > 300)
                {  // 超时转到长按状态，同时产生第一个按住事件
                    this.postEvent(this.keyId, ManKey.ModManKeyEvent.EVT_PRESSING);
                    this.m_pressingCount = 1;
                    this.m_state = ManKey.ModManKeyState.STT_LONG_PRESSING;
                }
                else if (keyPressState == false)
                {   // 完成一次点击，但需要延迟区分CLICK还是DBL_CLICK
                    this.m_state = ManKey.ModManKeyState.STT_WAITING_DBL_CLICK_DOWN;
                }
                break;
            case  ManKey.ModManKeyState.STT_WAITING_DBL_CLICK_DOWN:
                if (ms - this.m_downMs > 400)
                {   // 超出400毫秒没等到第二次按下，判断为CLICK结束，回归空闲
                    this.postEvent(this.keyId, ManKey.ModManKeyEvent.EVT_CLICK);
                    this.postEvent(this.keyId, ManKey.ModManKeyEvent.EVT_KEY_UP);
                    this.m_state = ManKey.ModManKeyState.STT_IDLE;
                }
                else if (keyPressState == true)
                {
                    this.m_state = ManKey.ModManKeyState.STT_WAITING_DBL_CLICK_UP;     // 遭遇再次按下，判断为DBL_CLICK，后续DBL_CLICK_UP
                }
                break;
            case  ManKey.ModManKeyState.STT_WAITING_DBL_CLICK_UP:
                if (keyPressState == false)
                {  // 简单等到再次抬起，结束DBL_CLICK，回归空闲
                    this.postEvent(this.keyId,  ManKey.ModManKeyEvent.EVT_DBL_CLICK);
                    this.postEvent(this.keyId,  ManKey.ModManKeyEvent.EVT_KEY_UP);
                    this.m_state =  ManKey.ModManKeyState.STT_IDLE;
                }
                break;
            case  ManKey.ModManKeyState.STT_LONG_PRESSING:
                if (keyPressState == false)
                {   // 长按期间等到抬起，结束LONG_CLICK，回归空闲
                    this.postEvent(this.keyId,  ManKey.ModManKeyEvent.EVT_LONG_CLICK);
                    this.postEvent(this.keyId,  ManKey.ModManKeyEvent.EVT_KEY_UP);
                    this.m_state =  ManKey.ModManKeyState.STT_IDLE;
                }
                else if (ms - this.m_downMs > 300 + this.m_pressingCount * 20)
                {
                    this.postEvent(this.keyId,  ManKey.ModManKeyEvent.EVT_PRESSING);
                    this.m_pressingCount++;
                }
                break;
        }
    }
}


